﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapICC_SRULES(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveICC_SRULES(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            Result = QueryICC_SRULES(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapICC_SRULES(ref dwLocal, dwData);

            if (!flgExist)
            {
                dwLocal["ROWNID"] = GetSeq();
                InsertICC_SRULES(dwLocal, ITXDBVERSION);
            }
            UpdateICC_SRULES(dwLocal);
        }
        public void InsertICC_SRULES(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            string table = "VVPACK";  //VVPMANDANT	VVPLIEFSCH	VVPPACKNR
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "50.24":
                    or1.CommandText = "INSERT INTO ICC_SRULES(ROWNID, MACHINEID,RGROUP, JOBV, JOBVDES,LOGIC,RVALUE,RULEDES)"
                                + " VALUES (:ROWNID, :MACHINEID,:RGROUP, :JOBV, :JOBVDES,:LOGIC,:RVALUE,:RULEDES)";
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    or1.Parameters.Add("MACHINEID", OracleType.NVarChar).Value = dwData["MACHINEID"].ToString();
                    or1.Parameters.Add("RGROUP", OracleType.NVarChar).Value = dwData["RGROUP"].ToString();
                    or1.Parameters.Add("JOBV", OracleType.NVarChar).Value = dwData["JOBV"].ToString();
                    or1.Parameters.Add("JOBVDES", OracleType.NVarChar).Value = dwData["JOBVDES"].ToString();
                    or1.Parameters.Add("LOGIC", OracleType.NVarChar).Value = dwData["LOGIC"].ToString();
                    or1.Parameters.Add("RVALUE", OracleType.NVarChar).Value = dwData["RVALUE"].ToString();
                    or1.Parameters.Add("RULEDES", OracleType.NVarChar).Value = dwData["RULEDES"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }

        public DataSet QueryICC_SRULES(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT * FROM ICC_SRULES WHERE   ROWNID=:ROWNID"; ;
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    break;
                case 9999:
                    or1.CommandText = "SELECT * FROM ICC_SRULES";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "ICC_SRULES";
            Return = Result;
            return Return;
        }
        public void UpdateICC_SRULES(DataRow dwData, int options = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (options)
            {
                case 1:
                    #region====Command====
                    or1.CommandText = "UPDATE ICC_SRULES SET "
                        + "MACHINEID=:MACHINEID,"
                        + "RGROUP=:RGROUP,"
                        + "JOBV=:JOBV,"
                        + "JOBVDES=:JOBVDES, "
                        + "LOGIC=:LOGIC,"
                        + "RVALUE=:RVALUE,"
                        + "RULEDES=:RULEDES "
                        + " WHERE  "
                        + "   ROWNID=:ROWNID ";
                    #endregion
                    #region====Parameters====
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    or1.Parameters.Add("MACHINEID", OracleType.NVarChar).Value = dwData["MACHINEID"].ToString();
                    or1.Parameters.Add("RGROUP", OracleType.NVarChar).Value = dwData["RGROUP"].ToString();
                    or1.Parameters.Add("JOBV", OracleType.NVarChar).Value = dwData["JOBV"].ToString();
                    or1.Parameters.Add("JOBVDES", OracleType.NVarChar).Value = dwData["JOBVDES"].ToString();
                    or1.Parameters.Add("LOGIC", OracleType.NVarChar).Value = dwData["LOGIC"].ToString();
                    or1.Parameters.Add("RVALUE", OracleType.NVarChar).Value = dwData["RVALUE"].ToString();
                    or1.Parameters.Add("RULEDES", OracleType.NVarChar).Value = dwData["RULEDES"].ToString();
                    #endregion
                    break;
            }

            Connect2Proxy(or1);
        }
    }
}
